package org.serc.pensieve.uicontroller;

/*
 * To change this template, choose Tools | Templates
 * and open the template in th

package Controllers;e editor.
 */

import org.serc.pensieve.uicontroller.UserRole;
import org.serc.pensieve.model.Admin;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.HTML;

/**
 *
 * @author Zauk
 */
public class AdminControllers extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        
        
           String filename = request.getParameter("file1");           
 
          System.out.println(filename);
 
           
           
           
           
           
           
           
           
           


        response.setContentType("text/html;charset=UTF-8");

         HttpSession  ss = request.getSession();

         String tempUname = (String)ss.getAttribute("userName");

         String tempUpass = (String)ss.getAttribute("userPassword");

         String tempUrole = (String)ss.getAttribute("userRole");

        //Creating object of user role and passing session value to it.
        UserRole userObj = new Admin(tempUname, tempUpass, tempUrole);
        //Getting Action from Admin pages(index.jsp).
        String adminAct = request.getParameter("action");

        PrintWriter out = response.getWriter();


         if(adminAct.equals("selectSRS"))
        {
     //  out.println("Yessssssssss");
            // System.out.println("wewdsdkojisdfksjkfhskdfjskdhkshdsjhd");
        ArrayList<String> tempReceivedUsers = new ArrayList<String>();
           tempReceivedUsers = userObj.SelectSRS();
        String htmlDD =    "<select id=\"dd1\"><option>Select SRS</option>";

        int length = tempReceivedUsers.size();
        
                for(int i = 0; i< length ; i++)
                {
                  //  if(!(i%2==0))
                    //{
                         htmlDD += "<option id=\""+tempReceivedUsers.get(i)+"\">"+tempReceivedUsers.get(i+1)+"</option>";
                    //}
                   
                i++;

                }
         htmlDD += "</select>";
         out.println(htmlDD);
         }
         else if(adminAct.equals("selectCheckList"))
        {
     //  out.println("Yessssssssss");
            // System.out.println("wewdsdkojisdfksjkfhskdfjskdhkshdsjhd");
        ArrayList<String> tempReceivedUser = new ArrayList<String>();
           tempReceivedUser = userObj.SelectCheckList();
        String htmlDD =    "<select id=\"dd1\"><option>Select Check List</option>";

        int length = tempReceivedUser.size();

                for(int i = 0; i< length ; i++)
                {
                    //if(!(i%2==0))
                    //{
                         htmlDD += "<option id=\""+tempReceivedUser.get(i)+"\">"+tempReceivedUser.get(i+1)+"</option>";
                    //}
                         i++;

                }
         htmlDD += "</select>";
         out.println(htmlDD);
         }
          else if(adminAct.equals("import_srs"))
        {
            
           int res = userObj.import_srs();
           
           out.println(res);
        }
            else if(adminAct.equals("import_checklist"))
        {
            
           int res = userObj.import_checklist();
           
           out.println(res);
        }
          else if(adminAct.equals("selectInspectors"))
        {
     //  out.println("Yessssssssss");
            // System.out.println("wewdsdkojisdfksjkfhskdfjskdhkshdsjhd");
        ArrayList<String> tempReceivedUser = new ArrayList<String>();
           tempReceivedUser = userObj.SelectInspectors();
        String htmlDD =    "";
   int i;
        int length = tempReceivedUser.size();
        int j=0;
        try
        {
                for(i = 0; i< length ; i++)
                {

                htmlDD += " <input type=\"checkbox\" name=\""+tempReceivedUser.get(i)+"\" id=\"c"+j+"\" value=\""+tempReceivedUser.get(i+1)+"\" style=\"padding-top:7px; text-align:center;\"/>  <span style=\"color:#333; text-align:center; margin-bottom:3px;\">&nbsp;&nbsp;&nbsp;"+tempReceivedUser.get(i+1)+"<br /></span>";
               // ss.setAttribute(tempReceivedUser.get(i), tempReceivedUser.get(i));
               // ss.setAttribute(tempReceivedUser.get(i+1), tempReceivedUser.get(i+1));
                j++;
                i++;

                }
               // ss.setAttribute("ins_length", i-1);
        }
          catch(Exception ex)
        {
           // System.out.print(zeshan);
        }
        // htmlDD += "</select>";
         htmlDD +="<input type=\"hidden\" id=\"inspectors_length\" value=\""+j+"\" /> ";
         out.println(htmlDD);
         }
            else if(adminAct.equals("selectModerators"))
        {
     //  out.println("Yessssssssss");
            // System.out.println("wewdsdkojisdfksjkfhskdfjskdhkshdsjhd");
        ArrayList<String> tempReceivedUser = new ArrayList<String>();
           tempReceivedUser = userObj.SelectModerators();
        String htmlDD =    "";

        int length = tempReceivedUser.size();
        int j=0;
        try
        {
                for(int i = 0; i< length ; i++)
                {

                    htmlDD += " <input type=\"checkbox\" name=\""+tempReceivedUser.get(i)+"\" id=\"d"+j+"\" value=\""+tempReceivedUser.get(i+1)+"\" style=\"padding-top:7px; text-align:center;\"/>  <span style=\"color:#333; text-align:center; margin-bottom:3px;\">&nbsp;&nbsp;&nbsp;"+tempReceivedUser.get(i+1)+"<br /></span>";
                    i++;
                    j++;
                }
        }
          catch(Exception ex)
        {
            System.out.print(ex);
        }
        // htmlDD += "</select>";
        htmlDD +="<input type=\"hidden\" id=\"moderators_length\" value=\""+j+"\" /> ";
         out.println(htmlDD);
         }
         else if(adminAct.equals("save_inspector"))
        {
            String hsrs_id = request.getParameter("hsrs_id");
            String hsrs_name = request.getParameter("hsrs_text");
            String hcheck_id = request.getParameter("hcheck_id");
            String hcheck_name = request.getParameter("hcheck_text");
            String lengthx = request.getParameter("ins_length");
            int length=Integer.parseInt(lengthx);
            String lengthy = request.getParameter("ins_length");
            int length1=Integer.parseInt(lengthy);
            ArrayList<String> arrSRS = new ArrayList<String>();
            ArrayList<String> arr1SRS = new ArrayList<String>();
            for(int i=0;i<length;i++)
            {
                String x="ins_"+i;
                String inspector = request.getParameter(x);
                arrSRS.add(inspector);
                i++;
                x="ins_"+i;
                inspector = request.getParameter(x);
                arrSRS.add(inspector);
            }
            int j=100;
            for(int i=0;i<length1;i++)
            {
                String x="mod_"+j;
                String moderator = request.getParameter(x);
                arr1SRS.add(moderator);
                i++;
                j++;
                x="mod_"+j;
                moderator = request.getParameter(x);
                arr1SRS.add(moderator);
                j++;
            }
            
            int check=userObj.SaveInspectors(hsrs_id,hsrs_name,hcheck_id,hcheck_name,arrSRS,arr1SRS);
           // userObj.SaveInspectors(hsrs_id, hsrs_name, hcheck_id, hcheck_name, arrSRS);
            if(check==1)
            {
                 response.sendRedirect("index.jsp");
            }
            
           
        }




 ///       PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AdminControllers</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AdminControllers at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
